package org.jeecg.modules.demo.hd.controller;

import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.hd.entity.AppUserCashDetail;
import org.jeecg.modules.demo.hd.service.IAppUserCashDetailService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.time.LocalDateTime;

/**
 * @Description: 用户现金明细
 * @Author: jeecg-boot
 * @Date: 2025-04-19
 * @Version: V1.0
 */
@Api(tags = "用户现金明细")
@RestController
@RequestMapping("/hd/appUserCashDetail")
@Slf4j
public class AppUserCashDetailController extends JeecgController<AppUserCashDetail, IAppUserCashDetailService> {
    @Autowired
    private IAppUserCashDetailService appUserCashDetailService;

    /**
     * 分页列表查询
     *
     * @param appUserCashDetail
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "用户现金明细-分页列表查询")
    @ApiOperation(value = "用户现金明细-分页列表查询", notes = "用户现金明细-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AppUserCashDetail>> queryPageList(AppUserCashDetail appUserCashDetail,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest req) {
        QueryWrapper<AppUserCashDetail> queryWrapper = QueryGenerator.initQueryWrapper(appUserCashDetail, req.getParameterMap());
        Page<AppUserCashDetail> page = new Page<AppUserCashDetail>(pageNo, pageSize);
        IPage<AppUserCashDetail> pageList = appUserCashDetailService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户现金明细-通过id删除")
    @ApiOperation(value = "用户现金明细-通过id删除", notes = "用户现金明细-通过id删除")
    @RequiresPermissions("hd:app_user_cash_detail:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        appUserCashDetailService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "用户现金明细-通过id查询")
    @ApiOperation(value = "用户现金明细-通过id查询", notes = "用户现金明细-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AppUserCashDetail> queryById(@RequestParam(name = "id", required = true) String id) {
        AppUserCashDetail appUserCashDetail = appUserCashDetailService.getById(id);
        if (appUserCashDetail == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(appUserCashDetail);
    }


    @ApiOperation(value = "用户金币获取详情列表查询-APP用户查询", notes = "用户金币获取详情列表查询")
    @GetMapping(value = "/currentList")
    public Result<IPage<AppUserCashDetail>> queryCurrentPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                 HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 计算7天前的日期
        LocalDateTime sevenDaysAgo = now.minusDays(7);
        Page<AppUserCashDetail> page = new Page<AppUserCashDetail>(pageNo, pageSize);
        LambdaQueryWrapper<AppUserCashDetail> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(AppUserCashDetail::getUserId, sysUser.getId())
                .ge(AppUserCashDetail::getBonusTime, sevenDaysAgo);
        IPage<AppUserCashDetail> pageList = appUserCashDetailService.page(page, lambdaQuery);
        return Result.OK(pageList);
    }
}
